class EducationalPlan < ActiveRecord::Base
  belongs_to :academic_degree
  belongs_to :educational_level
  belongs_to :term
  belongs_to :passing_grade, :class_name => 'Grade', :foreign_key => 'passing_grade_id', :conditions => 'is_passing_grade'
  
  has_many :subjects
  has_many :student_plan_enrollments

  validates_presence_of :name, :abbreviation, :academic_degree_id,
    :educational_level_id, :term_id, :passing_grade_id, :year, :total_credits,
    :duration_in_terms, :min_duration, :max_duration, :active
  validates_uniqueness_of :name
  validates_numericality_of :year, :only_integer => true, :greater_than => 1950
  validates_numericality_of :duration_in_terms, :only_integer => true, :greater_than => 0
  validates_numericality_of :max_duration, :only_integer => true, :greater_than => 0
  validates_numericality_of :min_duration, :only_integer => true, :greater_than => 0
  validate :max_greater_than_or_equal_to_min

  protected
  def max_greater_than_or_equal_to_min
    errors.add(:max_duration, 'debe de ser mayor o igual a la duración mínima') if max_duration.nil? ||
      max_duration < min_duration
  end
end
